package ar.com.mindset.geometrics_wi.colorScheme.divisionStrategy;

import ar.com.mindset.geometrics_wi.exceptions.GeometricsRuntimeException;

import java.util.HashMap;
import java.util.Map;


/**
 * @author http://www.mindset-designs.com
 */
public class ManualDivision extends DivisionStrategy {

    private String defaultColor;
    private Map<String, String> manualValues;


    public ManualDivision() {
        manualValues = new HashMap<String, String>();
    }

    public void setDefaultColor(String defaultColor) {
        this.defaultColor = defaultColor;
    }

    public void addDiscreteValue(String key, String color) {
        this.manualValues.put(key, color);
    }

    public void write(StringBuffer xml) {
        xml.append("<divisionStrategy> <manualDivision");

        if (this.defaultColor != null) {
            xml.append(" defaultColor='").append(this.defaultColor).append("'");
        }
        xml.append(">");

        this.writeDiscreteValues(xml);

        xml.append("</manualDivision></divisionStrategy>");
    }

    private void writeDiscreteValues(StringBuffer xml) throws GeometricsRuntimeException {
        if (manualValues.size() == 0) {
            throw new GeometricsRuntimeException("ManualDivision must have values. Try manualDivision.addDiscreteValue(String, String) matching values for each region");
        }

        for (String key : this.manualValues.keySet()) {
            xml.append("<colorEntry key='").append(key).append("' color='").append(this.manualValues.get(key)).append("'/>");
        }
    }
}
